package Thread;
public class ThreadDemo13 {
    static Object object = new Object();
    static int i = 0;
    public static void print1() throws InterruptedException {
         System.out.println("我锁住了要休眠十秒");
        synchronized (object) {
            //当i = 0 时才new,这里只让new一次
          if(i++ == 0)
            object = new Object();
            Thread.sleep(5000);
        }
        System.out.println("我释放锁了");
    }
    public static void print2() throws InterruptedException {
        synchronized (object) {
            System.out.println("我在print1没有释放锁的时候进来了");
        }
    }
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            try {
                print1();
                print1();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        Thread t2 = new Thread(()->{
            try {
                print2();
                Thread.sleep(6000);
                System.out.println("");
                print2();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t1.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
    while (i == 0){
        i--;
    }
    }
}